﻿Imports System.Reflection
Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
    ' 以下事件可用于 MyApplication: 
    ' Startup:应用程序启动时在创建启动窗体之前引发。
    ' Shutdown:在关闭所有应用程序窗体后引发。如果应用程序非正常终止，则不会引发此事件。
    ' UnhandledException:在应用程序遇到未经处理的异常时引发。
    ' StartupNextInstance:在启动单实例应用程序且应用程序已处于活动状态时引发。 
    ' NetworkAvailabilityChanged:在连接或断开网络连接时引发。
    Partial Friend Class MyApplication
        Public Sub Startup_Event(sender As Object, e As StartupEventArgs) Handles Me.Startup
            Sy.SetHandle(Application)
            Dim OnlineID As Integer
            DealParameter = Function(URLAndName As Sy.String)
                                Dim Spliter = "#"
                                If URLAndName.Contains(Spliter) Then
                                    Dim s = URLAndName.Trim.Split(Spliter)
                                    If s.Length = 4 Then
                                        Dim Url = s(0)
                                        Dim Name = s(1)
                                        OnlineID = s(2)
                                        LoginToken = s(3)
                                        Mod_GlobalVariables.URL = Url
                                        Mod_GlobalVariables.Name = Name
                                        Debug.Print(URLAndName)
                                    Else
                                        MsgBox($"客户名称出现分隔符,或存在其他未处理异常{URLAndName}", "Error")
                                    End If
                                End If
                                Return False
                            End Function
            Dim CheckPath = "check"
            Sy.Update.Server = "http://update.yimisoft.com/"
            LoginPath = "Login"
            GetCheckUrl = Function() New Uri($"{LoginAddress}{CheckPath}?OEM={SoftNameUrlEncode}&Oid={OnlineID}&UserName={UserName}&LoginTime={LoginTime}")
            SetOEMInfo()
            If Login() Then
                Sy.Update.CheckVersion()
                MainForm = Global_Frm_Main
                Mod_GlobalVariables.MainForm = Global_Frm_Main
            Else
                e.Cancel = True
                Environment.Exit(0)
            End If

            ' 在加载程序集时发生。(加载成功触发?)
            AddHandler AppDomain.CurrentDomain.AssemblyLoad,
                Sub(LoadSender As Object, args As AssemblyLoadEventArgs)
                    Debug.Print($"AssemblyLoad:{args.LoadedAssembly.Location}")
                End Sub
            ' 当程序集的解析在仅限反射的上下文中失败时发生。
            AddHandler AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve,
                Function(ResolveSender As Object, args As ResolveEventArgs) As Assembly
                    ' Dim AssemblyName name = New AssemblyName(args.Name);
                    ' Dim asmToCheck As String = Path.GetDirectoryName(m_rootAssembly) + "\\" + name.Name + ".dll"
                    'If (File.Exists(asmToCheck)) Then
                    '            {
                    'Return Assembly.ReflectionOnlyLoadFrom(asmToCheck);
                    '}
                    'MsgBox("ReflectionOnlyAssemblyResolve")
                    'Throw New NotImplementedException
                    'Return Assembly.ReflectionOnlyLoad(args.Name)
                    Debug.Print($"ReflectionOnlyAssemblyResolve:{args.Name}")
                    Return Nothing
                End Function
            '在对程序集的解析失败时发生。
            AddHandler AppDomain.CurrentDomain.AssemblyResolve,
                Function(ResolveSender As Object, args As ResolveEventArgs) As Assembly
                    'Dim name As AssemblyName = New AssemblyName(args.Name)
                    '' Dim asmToCheck As String = Path.GetDirectoryName(m_rootAssembly) + "\\" + name.Name + ".dll"
                    'Dim Folders = {"lib", "lib\cef", "lib\mstsc", "lib\npoi", "lib\updatecaller", "update"}
                    'For Each Folder In Folders
                    '    Dim AsmPath = $"{Folder}\{name.Name}.dll"
                    '    If IO.File.Exists(AsmPath) Then
                    '        Return Assembly.LoadFrom(AsmPath)
                    '    End If
                    'Next
                    'MsgBox($"未能加载{name.Name}")
                    'Environment.Exit(0)
                    'Throw New NotImplementedException
                    Debug.Print($"AssemblyResolve:{args.Name}")
                    Return Nothing
                End Function
        End Sub
    End Class
End Namespace
